package com.paireach.bw.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.utils.NetworkUtils;
import com.paireach.bw.R;
import com.paireach.bw.activity.BackActivity;
import com.paireach.bw.activity.CommonActivity;
import com.paireach.bw.activity.KaActivity;
import com.paireach.bw.activity.ServiceEvaluationActivity;
import com.paireach.bw.adapter.Frag2Adapter;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.JsonParse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2017/2/16.
 */

public class Frag2 extends BaseFragment implements Frag2Adapter.OnItemClickListener {

    private static final String TAG = "Frag2";
    private View rootView;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private List<DriveOrder> orderList = new ArrayList<>();
    private Frag2Adapter adapter;
    private Intent intent220;
    private Intent intent226;
    private Intent intent228;
    private Intent intent230;
    private Intent intent238;
    private Intent intent240;
    private boolean isViewCreated = false;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isViewCreated) {
            refreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    refreshLayout.setRefreshing(true);
                    refreshListener.onRefresh();
                }
            });
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag2, container, false);
        findViews();
        initViews();
        return rootView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshLayout.setRefreshing(true);
        refreshListener.onRefresh();
    }

    private void findViews() {
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refreshLayout);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    }

    private void initViews() {
        refreshLayout.setColorSchemeResources(R.color.item3, R.color.item11, R.color.gold);
        refreshLayout.setOnRefreshListener(refreshListener);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL));
        adapter = new Frag2Adapter(orderList);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(this);
    }

    private SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new WayBillRequest().onTheRoadOrderList(phoneNumber);
        }
    };


    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.ON_THE_ROAD_ORDER_LIST) {
            refreshLayout.setRefreshing(false);
            JsonParse.parseOnTheRoadOrderList(event.getJson());
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.ON_THE_ROAD_ORDER_LIST) {
            dismissProgressDialog();
            refreshLayout.setRefreshing(false);
            orderList.clear();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("on_the_road_order_list")) {
            if (event.getCode() == 0) {
                orderList.clear();
                adapter.notifyDataSetChanged();
                BDTraceUtil.closeTrace();
            } else if (event.getCode() == 1) {
                List<DriveOrder> orders = (List<DriveOrder>) event.getObj1();
                BDTraceUtil.openTrace();
                orderList.clear();
                orderList.addAll(orders);
                adapter.notifyDataSetChanged();
            } else {
                showToast(event.getExtraInfo());
            }
        }
    }

    //    KA KA
//    COMMON 常规
//    BACK 回瓶
    @Override
    public void onItemClick(View view, int position) {
        if (NetworkUtils.isConnected()) {
            DriveOrder driverOrder = orderList.get(position);
            String shpm_status = driverOrder.getSTATUS();
            String transport_code = driverOrder.getTRANSPORT_CODE();
            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
            switch (shpm_status) {
                case "220":
                    if ("BACK".equals(transport_code)) {
                        intent220 = new Intent(context, BackActivity.class);
                    } else if ("COMMON".equals(transport_code)) {
                        intent220 = new Intent(context, CommonActivity.class);
                    } else {
                        intent220 = new Intent(context, KaActivity.class);
                    }
                    intent220.putExtra("orderCode", driverOrder.getCODE());
                    ActivityCompat.startActivity(getActivity(), intent220, options.toBundle());
                    break;
                case "226":
                    if ("BACK".equals(transport_code)) {
                        intent226 = new Intent(context, BackActivity.class);
                    } else if ("COMMON".equals(transport_code)) {
                        intent226 = new Intent(context, CommonActivity.class);
                    } else {
                        intent226 = new Intent(context, KaActivity.class);
                    }
                    intent226.putExtra("orderCode", driverOrder.getCODE());
                    startActivity(intent226);
                    break;
                case "228"://发货地： 出厂
                    if ("BACK".equals(transport_code)) {
                        intent228 = new Intent(context, BackActivity.class);
                    } else if ("COMMON".equals(transport_code)) {
                        intent228 = new Intent(context, CommonActivity.class);
                    } else {
                        intent228 = new Intent(context, KaActivity.class);
                    }
                    intent228.putExtra("orderCode", driverOrder.getCODE());
                    startActivity(intent228);
                    break;
                case "230":
                    if ("BACK".equals(transport_code)) {//回瓶
                        intent230 = new Intent(context, BackActivity.class);
                    } else if ("KA".equals(transport_code)) {//KA的负载单状态也不变了，直到交货单全部走完，直接走245
                        intent230 = new Intent(context, KaActivity.class);
                    } else {//常规的负载单状态不变了，直到交货单全部走完，直接走245
                        intent230 = new Intent(context, CommonActivity.class);
                    }
                    intent230.putExtra("orderCode", driverOrder.getCODE());
                    startActivity(intent230);
                    break;
                case "245":
                    ActivityOptionsCompat options245 = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity());
                    Intent intent245 = new Intent(context, ServiceEvaluationActivity.class);
                    intent245.putExtra("orderCode", driverOrder.getCODE());
                    ActivityCompat.startActivity(getActivity(), intent245, options245.toBundle());
                    break;
            }
        } else {
            showToast("网络连接不可用，请稍后重试");
        }
    }

}
